<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="title" :visible.sync="showDialog" width="30%" @close="cancel">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form
      ref="form"
      label-width="120px"
      :model="formData"
      :rules="formRules"
    >
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="formData.username"
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="formData.mobile"
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" type="date" placeholder="选择日期" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" style="width: 240px">
          <el-option v-for="item in hireType" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          v-model="formData.workNumber"
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-cascader
          v-model="formData.departmentName"
          :options="departs"
          :props="{label:'name',value:'name'}"
        />
      </el-form-item>
      <el-form-item label="转正时间">
        <el-date-picker v-model="formData.correctionTime" type="date" placeholder="选择日期" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button
          type="primary"
          size="small"
          @click="btnOK"
        >确定</el-button>
        <el-button size="small" @click="isShow = false">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { tranListToTreeData } from '@/utils'
import { getDepartmentListData } from '@/api/department'
import { addUserData } from '@/api/employees'
import employeesConstant from '@/api/constant/employees'
export default {
  props: {
    nodeData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      hireType: employeesConstant.hireType,
      departs: [],
      employeesList: [],
      showDialog: false,
      formData: {
        // name: '', // 部门名称
        // code: '', // 部门编码
        // manager: '', // 部门管理者
        // introduce: '' // 部门介绍
      },
      formRules: {
        username: [{ required: true, message: '请输入', trigger: 'blur' }],
        mobile: [{ required: true, message: '请输入', trigger: 'blur' }],
        timeOfEntry: [{ required: true, message: '请输入', trigger: 'change' }],
        formOfEmployment: [{ required: true, message: '请输入', trigger: 'blur' }],
        workNumber: [{ required: true, message: '请输入', trigger: 'blur' }],
        departmentName: [{ required: true, type: 'array', message: '请输入', trigger: 'blur' }]
      }
    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改部门' : '添加部门'
    }
  },
  created() {
    this.getDeptsData()
  },
  methods: {
    async getDeptsData() {
      const { depts } = await getDepartmentListData()
      this.departs = tranListToTreeData(depts, '')
    },
    async btnOK() {
      await this.$refs.form.validate()
      // 加工拷贝数据 发送请求
      const departmentName = this.formData.departmentName
      await addUserData({
        ...this.formData,
        departmentName: departmentName[departmentName.length - 1]
      })
      this.$message.success('新增成功')
      this.showDialog = false
      this.$emit('update')
    },
    // 取消
    cancel() {
      this.formData = {}
      this.$refs.form.clearValidate()
    }
  }
}
</script>

<style>
</style>
